package com.gyz.cfmall.controller;

import com.gyz.cfmall.interceptor.CartInterceptor;
import com.gyz.cfmall.service.CartService;
import com.gyz.cfmall.to.UserInfoTo;
import com.gyz.cfmall.vo.CartItemVo;
import com.gyz.cfmall.vo.CartVo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import javax.annotation.Resource;
import java.util.concurrent.ExecutionException;

/**
 * @author gong_yz
 * @Description
 * @Date 2023/4/18
 */

@Controller
public class CartController {

    @Resource
    private CartService cartService;

    @GetMapping("/cartList.html")
    public String listPage() {
        UserInfoTo userInfoTo = CartInterceptor.threadLocal.get();
        System.out.println(userInfoTo);
        return "cartList";
    }

    @GetMapping("/success.html")
    public String listSuccessPage() {
        return "success";
    }

    /**
     * 去购物车页面的请求
     * 浏览器有一个cookie:user-key 标识用户的身份，一个月过期
     * 如果第一次使用jd的购物车功能，都会给一个临时的用户身份:
     * 浏览器以后保存，每次访问都会带上这个cookie；
     * <p>
     * 登录：session有
     * 没登录：按照cookie里面带来user-key来做
     * 第一次，如果没有临时用户，自动创建一个临时用户
     *
     * @return
     */
    @GetMapping(value = "/cart.html")
    public String cartListPage(Model model) throws ExecutionException, InterruptedException {
        //快速得到用户信息：id,user-key
        // UserInfoTo userInfoTo = CartInterceptor.toThreadLocal.get();

        CartVo cartVo = cartService.getCart();
        model.addAttribute("cart", cartVo);
        return "cartList";
    }

    @GetMapping("/addCartItem")
    public String addCartItem(@RequestParam("skuId") Long skuId,
                              @RequestParam("num") Integer num,
                              Model model,
                              RedirectAttributes redirectAttributes) throws ExecutionException, InterruptedException {
        CartItemVo cartItemVo = cartService.addCartItem(skuId, num);
//        model.addAttribute("cartItem", cartItemVo);
        redirectAttributes.addAttribute("skuId", skuId);
        return "redirect:http://cart.cfmall.com/addCartItmSuccess.html";
    }


    @GetMapping("/addCartItmSuccess.html")
    public String addCartItmSuccess(@RequestParam("skuId") Long skuId, Model model) {
        CartItemVo cartItemOne = cartService.getCartItem(skuId);
        model.addAttribute("cartItem", cartItemOne);
        return "success";
    }

    /**
     * 商品是否选中
     *
     * @param skuId
     * @param checked
     * @return
     */
    @GetMapping(value = "/checkItem")
    public String checkItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "checked") Integer checked) {
        cartService.checkItem(skuId, checked);

        return "redirect:http://cart.cfmall.com/cartList.html";
    }

    /**
     * 改变商品数量
     *
     * @param skuId
     * @param num
     * @return
     */
    @GetMapping(value = "/countItem")
    public String countItem(@RequestParam(value = "skuId") Long skuId,
                            @RequestParam(value = "num") Integer num) {

        cartService.changeItemCount(skuId, num);
        return "redirect:http://cart.cfmall.com/cart.html";
    }

    /**
     * 删除商品信息
     *
     * @param skuId
     * @return
     */
    @GetMapping(value = "/deleteItem")
    public String deleteItem(@RequestParam("skuId") Integer skuId) {

        cartService.deleteIdCartInfo(skuId);
        return "redirect:http://cart.cfmall.com/cart.html";

    }

}
